<?php
/*
 * Glorious Framework
 *
 * Form helper functions
 *
 * (c) 2007-2010 Konspire Design
 *
 * $Id: form_helper.php,v 1334330a27b3 2010/05/05 21:14:16 and $
 * $Author: and $
 * $Date: 2010/05/05 21:14:16 $
*/

load_helper('nonce');

function atts_to_string($atts) {
	$str = '';

	if(is_array($atts))
		foreach ($atts as $key => $val)
			if( isset( $val ) )
				$str .= ' ' . $key . '="' . $val . '"';

	return $str;
}

function form_prep($str = '') {
	if (is_array($str)) {
		foreach ($str as $key => $val)
			$str[$key] = form_prep($val);

		return $str;
	}

	if ($str === '')
		return '';

	$str = htmlspecialchars($str);

	// In case htmlspecialchars misses these.
	//$str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);

	return $str;
}

function form_open($action = '', $atts = array()) {
	$atts = _::parse_args($atts, array('method' => 'post'));

	$action = (strpos($action, '://') === false) ? site_url($action) : $action;

	$form = '<form action="' . $action . '"';
	$form .= atts_to_string($atts);
	$form .= ">\n";

	return $form;
}

function form_security($action = -1) {
	$hidden = form_hidden(array('name' => 'action', 'value' => $action));
	$hidden .= nonce_field($action);

	return $hidden;
}

function form_open_multipart($action = '', $atts = array()) {
	return form_open($action, _::parse_args($atts, array('enctype' => 'multipart/form-data')));
}

function form_close() {
	return '</form>';
}

function form_label($label_text = '', $atts = array()) {
	$label = '<label';
	$label .= atts_to_string(_::parse_args($atts));
	$label .= ">$label_text</label>\n";

	return $label;
}

function form_input($atts = array()) {
	$atts = _::parse_args($atts,
			array(
				'type' => 'text',
				'name' => null,
				'value' => ''));
	
	return "<input" . atts_to_string($atts) . " />\n";
}

function form_password($atts = array()) {
	return form_input(_::parse_args($atts, array('type' => 'password')));
}

function form_hidden($atts = array()) {
	return form_input(_::parse_args($atts, array('type' => 'hidden')));
}

function form_upload($atts = array()) {
	return form_input(_::parse_args($atts, array('type' => 'file')));
}

function form_textarea($text, $atts = array()) {
	$val = '';
	$atts = _::parse_args($atts, array('cols' => '90', 'rows' => '12'));

	return "<textarea" . atts_to_string($atts) . ">" . form_prep($text) . "</textarea>";
}

function form_select($atts = array(), $options = array(), $selected = array()) {
	$atts = _::parse_args($atts);

	if(!is_array($selected))
		$selected = array($selected);

	$form = '<select' . atts_to_string($atts) . ">\n";

	foreach ($options as $key => $val) {
		$key = (string) $key;

		if (is_array($val)) {
			$form .= '<optgroup label="'.$key.'">'."\n";

			foreach ($val as $optgroup_key => $optgroup_val) {
				$sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';

				$form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
			}

			$form .= '</optgroup>'."\n";
		}
		else {
			$sel = (in_array($key, $selected)) ? ' selected="selected"' : '';

			$form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
		}
	}

	$form .= '</select>';

	return $form;
}

function form_multiselect($atts = array(), $options = array(), $selected = array()) {
	return form_select(_::parse_args($atts, array('multiple' => 'multiple')), $options, $selected);
}

function form_checkbox($atts = array()) {
	$atts = _::parse_args($atts, array('type' => 'checkbox'));

	if(isset($atts['checked'])) {
		if($atts['checked'])
			$atts['checked'] = 'checked';
		else
			unset($atts['checked']);
	}

	return "<input" . atts_to_string($atts) . " />\n";
}

function form_radio($atts = array()) {
	return form_checkbox(_::parse_args($atts, array('type' => 'radio')));
}

function form_button($atts = array()) {
	return form_input(_::parse_args($atts, array('type' => 'button')));
}

function form_submit($atts = array()) {
	return form_input(_::parse_args($atts, array('type' => 'submit')));
}

function form_reset($atts = array()) {
	return form_input(_::parse_args($atts, array('type' => 'reset')));
}

function form_fieldset($legend_text = '', $atts = array()) {
	$fieldset = "<fieldset";
	$fieldset .= atts_to_string($atts);
	$fieldset .= ">\n";

	if($legend_text != '')
		$fieldset .= "<legend>$legend_text</legend>\n";

	return $fieldset;
}

function form_fieldset_close() {
	return '</fieldset>';
}

function &array_traverse($name, &$array, $default = false) {
	$v = array();

	if(is_string($name))
		parse_str($name, $v);

	if(is_array($array) && is_array($v)) {
		$ref =& $array;

		while(is_array($v)) {
			$first_key = key($v);

			if(!is_null($first_key) && isset($ref[$first_key])) {
				$ref =& $ref[$first_key];
				$v = $v[$first_key];
			} else
				return $default;
		}

		return $ref;
	}

	return $default;
}

function array_traverse_find($name, &$array)
{
	$v = array();
	$r = array();

	if(is_string($name))
		parse_str($name, $v);

	if(!is_array($array) || !is_array($v))
		return null;

	$ref =& $array;

	while(is_array($v)) {
		$first_key = key($v);

		if(!is_null($first_key) && isset($ref[$first_key])) {
			$ref =& $ref[$first_key];
			$r[] = $first_key;
		} else
			break;
	}

	return join('-',$r);
}

// cast lol[wtf][zoo] strings to :array type with keys
function array_traverse_set($name, &$array, $value)
{
	$v = array();
	
	if(is_string($name))
	{
		if(empty($name))
		{
			$array = $value;
			return;
		}
		parse_str($name, $v);
	}

	if(!is_array($v))
		return;
	
	if(!is_array($array))
		$array = array( );
	
	$ref =& $array;

	while(is_array($v)) {
		$first_key = key($v);

		if(is_null($first_key) || !isset($ref[$first_key]))
		{
			$ref[$first_key] = array( );
		}

		$ref =& $ref[$first_key];
		$v = $v[$first_key];
	}

	$ref = $value;
}





function form_value($field, $default = '', $where = 'post') {

	$var =& $_POST;

	if($where == 'get')
		$var =& $_GET;

	$value = array_traverse($field, $var, null);

	if(is_null($value))
		return $default;

	return form_prep($value);
}

function form_message($message, $status = 'updated') {
	return '<p class="' . $status . '">' . $message . '</p>';
}

/* End of file form_helper.php */
/* Location: ./system/helpers/form_helper.php */